package utils;

import formes.Forme;
import utils.LinkedList;

public class Sorting
{

    /**
     * 
     * @param list
     *            la linkedlist contenant les form
     * @return la liste trier par odre de id croissant
     */
    public static LinkedList sortIdCroissant(LinkedList list)
    {
        Forme tempForme;
        for (int i = 0; i < 10; i++) {
            for (int j = 1; j < (10 - i); j++) {
                if (list.get(j - 1).getForm().getId() > list.get(j).getForm()
                        .getId()) {
                    tempForme = list.get(j - 1).getForm();
                    list.get(j - 1).setForm(list.get(j).getForm());
                    list.get(j).setForm(tempForme);
                }
            }
        }
        return list;
    }

    /**
     * @param list
     *            la linkedlist contenant les form
     * @return la liste trier par odre de id decroissant
     */
    public static LinkedList sortIdDeroissant(LinkedList list)
    {
        Forme tempForme;
        for (int i = 0; i < 10; i++) {
            for (int j = 1; j < (10 - i); j++) {
                if (list.get(j - 1).getForm().getId() < list.get(j).getForm()
                        .getId()) {
                    tempForme = list.get(j - 1).getForm();
                    list.get(j - 1).setForm(list.get(j).getForm());
                    list.get(j).setForm(tempForme);
                }
            }
        }
        return list;
    }

    /**
     * @param list
     *            la linkedlist contenant les form
     * @return la liste trier par odre d'aire croissant
     */
    public static LinkedList sortAireCroissant(LinkedList list)
    {
        Forme tempForme;
        for (int i = 0; i < 10; i++) {
            for (int j = 1; j < (10 - i); j++) {
                if (list.get(j - 1).getForm().calculAire() > list.get(j)
                        .getForm().calculAire()) {
                    tempForme = list.get(j - 1).getForm();
                    list.get(j - 1).setForm(list.get(j).getForm());
                    list.get(j).setForm(tempForme);
                }
            }
        }
        return list;
    }

    /**
     * @param list
     *            la linkedlist contenant les form
     * @return la liste trier par aire decroissant
     */
    public static LinkedList sortAireDecroissant(LinkedList list)
    {
        Forme tempForme;
        for (int i = 0; i < 10; i++) {
            for (int j = 1; j < (10 - i); j++) {
                if (list.get(j - 1).getForm().calculAire() < list.get(j)
                        .getForm().calculAire()) {
                    tempForme = list.get(j - 1).getForm();
                    list.get(j - 1).setForm(list.get(j).getForm());
                    list.get(j).setForm(tempForme);
                }
            }
        }
        return list;
    }

    /**
     * @param list
     *            la linkedlist contenant les form
     * @return la liste trier par distance croissante entre deux point
     */
    public static LinkedList sortDistance(LinkedList list)
    {
        Forme tempForme;
        for (int i = 0; i < 10; i++) {
            for (int j = 1; j < (10 - i); j++) {
                if (list.get(j - 1).getForm().calculLigne() > list.get(j)
                        .getForm().calculLigne()) {
                    tempForme = list.get(j - 1).getForm();
                    list.get(j - 1).setForm(list.get(j).getForm());
                    list.get(j).setForm(tempForme);
                }
            }
        }
        return list;
    }

    /**
     * @param list
     *            la linkedlist contenant les form
     * @return la liste trier par distance dans l'ordre carré, rectangle,
     *         cercle, ovale, ligne
     */
    public static LinkedList sortCarreLigne(LinkedList list)
    {
        int current = 0;
        int next = 0;
        Forme tempForme;

        for (int i = 0; i < 10; i++) {
            for (int j = 1; j < (10 - i); j++) {

                current = determinerType(list.get(j - 1).getForm().getType());
                next = determinerType(list.get(j).getForm().getType());

                if (current > next) {
                    tempForme = list.get(j - 1).getForm();
                    list.get(j - 1).setForm(list.get(j).getForm());
                    list.get(j).setForm(tempForme);
                }
            }
        }
        return list;
    }

    /**
     * @param list
     *            la linkedlist contenant les form
     * @return la liste trier par distance dans l'ordre ligne, ovale, cercle,
     *         rectangle, carré
     */
    public static LinkedList sortLigneCarre(LinkedList list)
    {
        int current = 0;
        int next = 0;
        Forme tempForme;

        for (int i = 0; i < 10; i++) {
            for (int j = 1; j < (10 - i); j++) {

                current = determinerType(list.get(j - 1).getForm().getType());
                next = determinerType(list.get(j).getForm().getType());

                if (current < next) {
                    tempForme = list.get(j - 1).getForm();
                    list.get(j - 1).setForm(list.get(j).getForm());
                    list.get(j).setForm(tempForme);
                }
            }
        }
        return list;
    }

    public static int determinerType(String type)
    {
        if (type.equals("CARRE")) {
            return 1;
        } else if (type.equals("RECTANGLE")) {
            return 2;
        } else if (type.equals("CERCLE")) {
            return 3;
        } else if (type.equals("OVALE")) {
            return 4;
        } else if (type.equals("LIGNE")) {
            return 5;
        }       
        return 999;
    }
}
/**
 * if (order.equals("IdCroissant")) {
 * 
 * } else if (order.equals("IdDecroissant")) {
 * 
 * } else if (order.equals("AireCroissant")) {
 * 
 * } else if (order.equals("AireDecroissant")) {
 * 
 * }else if (order.equals("Ordre")) {
 * 
 * }else if (order.equals("OrdreInverse")) {
 * 
 * }else if (order.equals("Distance")) {
 * 
 * } else {
 * 
 * }
 * 
 * Forme tempForme;
 * 
 * LinkedList current = list;
 * 
 * for (int i = 0; i < 10; i++) { for (int j = 1; j < (10 - i); j++) {
 * if(list.getForm().getId() > list.next().getForm().getId()) //
 * if(list.getForm().getId() < list.next().getForm().getId()) { tempForme =
 * list.getForm(); list.setForm(list.next().getForm());
 * list.next().setForm(tempForme); } } }
 * 
 * 
 * 
 * Forme tempForme;
 * 
 * boolean sorted; LinkedList next; LinkedList current;
 * 
 * current = list; next = current.next();
 * 
 * while (current.next() != null) { if(current.getForm().getId() <
 * next.getForm().getId()) { tempForme = current.getForm();
 * current.setForm(next.getForm()); next.setForm(tempForme); sorted= false; }
 * current = current.next(); }
 * 
 * return list;
 */
